#include "mainwindow.h"
#include "ui_mainwindow.h"

/*!
 \brief

 \param parent
*/
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    tmp = "";

    QObject::connect(ui->plusButton, SIGNAL(clicked()), this, SLOT(Addition()));
    QObject::connect(ui->minusButton,SIGNAL(clicked()), this, SLOT(Subtraction()));
    QObject::connect(ui->multiplyButton,SIGNAL(clicked()), this, SLOT(Multiplication()));
    QObject::connect(ui->divisionButton,SIGNAL(clicked()), this, SLOT(Division()));
    QObject::connect(ui->exitButton,SIGNAL(clicked()), this, SLOT(Exit()));
    QObject::connect(ui->bClear,SIGNAL(clicked()), this, SLOT(ClearAll()));
    QObject::connect(ui->b0,SIGNAL(clicked()), this, SLOT(AddText0()));
    QObject::connect(ui->b1,SIGNAL(clicked()), this, SLOT(AddText1()));
    QObject::connect(ui->b2,SIGNAL(clicked()), this, SLOT(AddText2()));
    QObject::connect(ui->b3,SIGNAL(clicked()), this, SLOT(AddText3()));
    QObject::connect(ui->b4,SIGNAL(clicked()), this, SLOT(AddText4()));
    QObject::connect(ui->b5,SIGNAL(clicked()), this, SLOT(AddText5()));
    QObject::connect(ui->b6,SIGNAL(clicked()), this, SLOT(AddText6()));
    QObject::connect(ui->b7,SIGNAL(clicked()), this, SLOT(AddText7()));
    QObject::connect(ui->b8,SIGNAL(clicked()), this, SLOT(AddText8()));
    QObject::connect(ui->b9,SIGNAL(clicked()), this, SLOT(AddText9()));
}

/*!
 \brief

*/
MainWindow::~MainWindow()
{
    delete ui;
}

/*!
 \brief

*/
void MainWindow::Addition()
{
    QString strFisrt = ui->lineEditNumber1->text();
    QString strSecond = ui->lineEditNumber2->text();

    bool ok;
    int number1 = strFisrt.toInt(&ok);
    int number2 = strSecond.toInt(&ok);
    int result = number1 + number2;

    QString strResult;
    strResult = strResult.number(result);
    ui->lineEditResult->setText(strResult);
    ui->toDo->setText("+");
}

/*!
 \brief

*/
void MainWindow::Subtraction()
{
    QString strFisrt = ui->lineEditNumber1->text();
    QString strSecond = ui->lineEditNumber2->text();

    bool ok;
    int number1 = strFisrt.toInt(&ok);
    int number2 = strSecond.toInt(&ok);
    int result = number1 - number2;

    QString strResult;
    strResult = strResult.number(result);
    ui->lineEditResult->setText(strResult);
    ui->toDo->setText("-");
}

/*!
 \brief

*/
void MainWindow::Multiplication()
{
    QString strFisrt = ui->lineEditNumber1->text();
    QString strSecond = ui->lineEditNumber2->text();

    bool ok;
    int number1 = strFisrt.toInt(&ok);
    int number2 = strSecond.toInt(&ok);
    int result = number1 * number2;

    QString strResult;
    strResult = strResult.number(result);
    ui->lineEditResult->setText(strResult);
    ui->toDo->setText("*");
}

/*! jakis komentarz
 \brief

*/
void MainWindow::Division()
{
    QString strFisrt = ui->lineEditNumber1->text();
    QString strSecond = ui->lineEditNumber2->text();

    bool ok;
    int p;
    int number1 = strFisrt.toInt(&ok);
    int number2 = strSecond.toInt(&ok);

    if(number2 == 0)
    {
        ui->lineEditResult->setText("ERR");
        return;
    }

    int result = number1 / number2;

    QString strResult;
    strResult = strResult.number(result);
    ui->lineEditResult->setText(strResult);
    ui->toDo->setText("/");
}

/*!
 \brief

*/
void MainWindow::AddText0()
{
    this->SetText("0");
}

/*!
 \brief

*/
void MainWindow::AddText1()
{
    this->SetText("1");
}

/*!
 \brief

*/
void MainWindow::AddText2()
{
    this->SetText("2");
}

/*!
 \brief

*/
void MainWindow::AddText3()
{
    this->SetText("3");
}

/*!
 \brief

*/
void MainWindow::AddText4()
{
    this->SetText("4");
}

/*!
 \brief

*/
void MainWindow::AddText5()
{
    this->SetText("5");
}

/*!
 \brief

*/
void MainWindow::AddText6()
{
    this->SetText("6");
}

/*!
 \brief

*/
void MainWindow::AddText7()
{
    this->SetText("7");
}

/*!
 \brief

*/
void MainWindow::AddText8()
{
    this->SetText("8");
}

/*!
 \brief

*/
void MainWindow::AddText9()
{
    this->SetText("9");
}

/*!
 \brief

 \param s
*/
void MainWindow::SetText(QString s)
{
    if(ui->v1->isChecked())
    {
        tmp = ui->lineEditNumber1->text() + s;
        ui->lineEditNumber1->setText(tmp);
    }
    if(ui->v2->isChecked())
    {
        tmp = ui->lineEditNumber2->text() + s;
        ui->lineEditNumber2->setText(tmp);
    }
}

/*!
 \brief

*/
void MainWindow::ClearAll()
{
    ui->lineEditNumber1->setText("");
    ui->lineEditNumber2->setText("");
    ui->lineEditResult->setText("");
    ui->toDo->setText("?");
}

/*!
 \brief

*/
void MainWindow::Exit()
{
    this->Exit();
}
